package com.podcstr.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author azim
 * @since 5/9/13 
 */

public class MD5 {
    private static String toHexString(byte b) {
        int value = (b & 0x7F) + (b < 0 ? 128 : 0);

        String ret = (value < 16 ? "0" : "");
        ret += Integer.toHexString(value).toLowerCase();

        return ret;
    }

    public static String do_checksum(String data) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("SHA-256");
        StringBuffer strbuf = new StringBuffer();
        String output = null;
        byte[] digest = null;
        try {
            md5.update(data.getBytes("UTF-8"));
            digest = md5.digest();
            for (int i = 0; i < digest.length; i++) {
                strbuf.append(toHexString(digest[i]));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return strbuf.toString();
    }
}
